package com.neutech.maplestory.entity;

import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * ·@author：冯。
 * ·@version：v.1.0。
 * ·@docroot：表示产生文档的根路径。
 * ·@param：方法的参数类型。
 * ·@return：方法的返回类型。
 */
public class Hero extends AbstractMapleStoryObject{
    public static Image[] imgs = new Image[100];
    static{
        for (int i = 0; i < 4; i++) {
            imgs[i] = ImageUtil.getImage("hero_right_stand" + i);
        }
        for (int i = 4; i < 8; i++) {
            imgs[i] = ImageUtil.getImage("hero_left_stand" + (i - 4));
        }
        for (int i = 8; i < 13; i++) {
            imgs[i] = ImageUtil.getImage("hero_right_walk" + (i - 8));
        }
        for (int i = 13; i < 18; i++) {
            imgs[i] = ImageUtil.getImage("hero_left_walk" + (i - 13));
        }
        imgs[18] = ImageUtil.getImage("hero_right_prone");
        imgs[19] = ImageUtil.getImage("hero_left_prone");
        imgs[20] = ImageUtil.getImage("hero_right_jump");
        imgs[21] = ImageUtil.getImage("hero_left_jump");

    }

    public Hero() {
    }
    public Hero(int x,int y){
        this.x = x;
        this.y = y;
        this.speed = Constant.HERO_SPEED;
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        this.dir = Direction.RIGHT;
        this.action = Action.STAND;
    }

    /**
     * 计数器，一直增加
     */
    private int count;
    /**
     * 计算当前画第几张图
     */
    private int imgNum;
    @Override
    public void draw(Graphics g) {
        switch (dir){
            case RIGHT:
                switch (action){
                    case STAND:
                        //right stand
                        if (count++ % 5 == 0){
                            g.drawImage(imgs[imgNum++ % 4],x,y,null);
                        }
                        else{
                            g.drawImage(imgs[imgNum % 4],x,y,null);
                        }
                        break;
                    case WALK:
                        //right walk
                        if (count++ % 5 == 0){
                            g.drawImage(imgs[imgNum++ % 5 + 8],x,y,null);
                        }
                        else{
                            g.drawImage(imgs[imgNum % 5 + 8],x,y,null);
                        }
                        break;
                    case PRONE:
                        g.drawImage(imgs[18],x,y+30,null);
                        break;
                    case JUMP:
                        g.drawImage(imgs[20],x,y,null);
                        break;
                    default:
                        break;
                }
                break;
            case LEFT:
                switch (action){
                    case STAND:
                        //left stand
                        if (count++ % 5 == 0){
                            g.drawImage(imgs[imgNum++ % 4 + 4],x,y,null);
                        }else{
                            g.drawImage(imgs[imgNum % 4 + 4],x,y,null);
                        }
                        break;
                    case WALK:
                        //left walk
                        if (count++ % 5 == 0){
                            g.drawImage(imgs[imgNum++ % 4 + 13],x,y,null);
                        }
                        else{
                            g.drawImage(imgs[imgNum % 4 + 13],x,y,null);
                        }
                        break;
                    case PRONE:
                        g.drawImage(imgs[19],x,y+30,null);
                        break;
                    case JUMP:
                        g.drawImage(imgs[21],x,y,null);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        move();
    }

    public boolean left,right,down,jump;

    @Override
    public void move() {
        if (left){
            x -= speed;
            this.action = Action.WALK;
        }
        if (right) {
            x += speed;
            this.action = Action.WALK;
        }
        if(jump){
            jump();
        }
        confirmStatus();
    }

    private double v0 =Constant.INIT_JUMP_SPEED;
    private double vt;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.5;
    /**
     * 跳跃的方法
     */
    private void jump() {
        //vt = v0 + gt    同时完成竖直上抛和自由落体
        vt = v0 - g * t;
        delta_height = v0 * t;
        v0 = vt;
        y -= delta_height;
        //自由落体的结束位置
        if(y >= 500){
            jump = false;
            v0 = Constant.INIT_JUMP_SPEED;
            vt = 0.0;
            y = 500;
        }
    }


    /**
     * 根据当前人物的左右开关，确定人物的方向
     */
    private void confirmStatus(){
        if(left && !right){
            this.dir = Direction.LEFT;
            if(jump){
                this.action = Action.JUMP;
            }
        }else if(right && !left){
            this.dir = Direction.RIGHT;
            if(jump){
                this.action = Action.JUMP;
            }
        }else if (down){
            this.action = Action.PRONE;
        }else if (jump){
            this.action = Action.JUMP;
        }else {
            //都按下或都不按下站立
            this.action = Action.STAND;
        }
    }

    /**
     * 按键的方法
     * @param e 键盘事件对象
     */
    public void keyPressed(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_A :
                left = true;
                break;
            case KeyEvent.VK_D :
                right = true;
                break;
            case KeyEvent.VK_S:
                down = true;
                break;
            case KeyEvent.VK_K: //跳跃
                jump = true;
                break;
        }
    }

    /**
     * 抬起键的方法
     * @param e 键盘事件对象
     */
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()){
            case KeyEvent.VK_A :
                left = false;
                break;
            case KeyEvent.VK_D :
                right = false;
                break;
            case KeyEvent.VK_S:
                down = false;
                break;
/*            case KeyEvent.VK_K:
                jump = false;
                break;*/
        }
    }
}
